package com.carmaintain.servlets;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carmaintain.common.Constants;
import com.carmaintain.common.Utils;
import com.carmaintain.config.Configs;
import com.carmaintain.db.CommonLib;
import com.carmaintain.models.ResultObject;

/**
 * Servlet implementation class SearchPlaces
 */
public class SearchPlaces extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchPlaces() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ResultObject result = new ResultObject();
		Map<String, Object> places;
		String placeType = "";
		String keyword = "";
		float latitude = 0;
		float longtitude = 0;
		int page = 1;
		int pageSize = Configs.DEFAULT_PAGE_SIZE;
		
		try {
			request.setCharacterEncoding("UTF-8");
			if (!Utils.checkSign(request)) {
				result.setCode(Constants.CODE_SIGN_ERROR);
				result.setMessage(Constants.MESSAGE_WRONG_SIGN);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}
			if (!Utils.empty(request.getParameter("place_type"))) {
				placeType = request.getParameter("place_type");
			}
			if (!Utils.empty(request.getParameter("keyword"))) {
				keyword = request.getParameter("keyword");
			}
			if (!Utils.empty(request.getParameter("location"))) {
				latitude = Float.parseFloat(request.getParameter("location").split(",")[0]);
				longtitude = Float.parseFloat(request.getParameter("location").split(",")[1]);
			}
			if (!Utils.empty(request.getParameter("page"))) {
				try {
					page = Integer.parseInt(request.getParameter("page"));
					if (page < 1) {
						page = 1;
					}
				} catch (Exception e) {
					page = 1;
				}
			}
			if (!Utils.empty(request.getParameter("page_size"))) {
				try {
					pageSize = Integer.parseInt(request.getParameter("page_size"));
					if (pageSize < 1) {
						pageSize = Configs.DEFAULT_PAGE_SIZE;
					}
				} catch (Exception e) {
					pageSize = Configs.DEFAULT_PAGE_SIZE;
				}
			}
		} catch (Exception e) {
			result.setCode(Constants.CODE_WRONG_PARAMATER);
			result.setMessage(Constants.MESSAGE_WRONG_PARAMATER);
			Utils.addTestMessage(result, e);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(Utils.genJsonString(result));
			return;
		}
		
		try {
			places = CommonLib.searchPlaces(placeType, keyword, latitude, longtitude, page, pageSize);
			if (places == null) {
				result.setCode(Constants.CODE_FAIL);
				result.setMessage(Constants.MESSAGE_FAIL);
				result.setResultData(places);
			} else {
				result.setCode(Constants.CODE_SUCCESS);
				result.setMessage(Constants.MESSAGE_SUCCESS);
				result.setResultData(places);
			}
			
		} catch (Exception e) {
			result.setCode(Constants.CODE_EXCEPTION);
			result.setMessage(Constants.MESSAGE_EXCETION);
			Utils.addTestMessage(result, e);
		}
		
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(Utils.genJsonString(result));
	}

}
